package com.liruo.learn.spring.mvc.controller.mvc;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.http.RequestEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;

import javax.servlet.http.Part;

/**
 * @Author:liruo
 * @Date:2023-06-09-19:30:04
 * @Desc
 */
@RestController
@RequestMapping("/method/arg/test")
@Slf4j
public class MethodArgumentResolverControllerTest {
    @Data
    public static class ArgTest{
        private String value;

        private String formDataParam;
        //只能是MultipartFile, 因为是从MultipartRequest获取的
        private MultipartFile multipartFile;
    }
    //RequestParamMethodArgumentResolver
    @GetMapping("/1/noAnnotationCommon")
    public boolean f11(String arg){
        log.info("{}",arg);
        return true;
    }
    //ServletModelAttributeMethodProcessor
    @GetMapping("/1/noAnnotationBean")
    public boolean f12(ArgTest arg){
        log.info("{}",arg);
        return true;
    }
    //HttpEntityMethodProcessor
    @PostMapping("/2/requestEntity/no-annotation")
    public boolean f21(RequestEntity<String> arg){
        log.info("{} {}",arg.getBody(), arg);
        return true;
    }
    //HttpEntityMethodProcessor
    @PostMapping("/2/requestEntity/noAnnotationBean")
    public boolean f22(RequestEntity<ArgTest> arg){
        log.info("{} {}",arg.getBody(), arg);
        return true;
    }



    //RequestParamMethodArgumentResolver
    @GetMapping("/3/requestParam")
    public boolean f31(@RequestParam("arg") String arg){
        log.info("{}",arg);
        return true;
    }
    //PathVariableMethodArgumentResolver
    @GetMapping("/3/pathVariable/{arg}")
    public boolean f32(@PathVariable("arg") String arg){
        log.info("{}",arg);
        return true;
    }
    @GetMapping("/{intV}")
    public boolean test(@PathVariable("intV") Long intV){
        log.info("{}",intV);
        return true;
    }

    @Scope("prototype")
    @Bean
    public WebMvcConfigurer enableMatrixVariableWebMvcConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
                UrlPathHelper helper = new UrlPathHelper();
                helper.setRemoveSemicolonContent(false);
                configurer.setUrlPathHelper(helper);
            }
        };
    }
    //MatrixVariableMethodArgumentResolver
    //HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE表示MatrixVariable过时了, swagger也不支持
    //  如果需要, 需要在WebMvcConfigurer配置configurePathMatch new UrlPathHelper().setRemoveSemicolonContent(false)
    @GetMapping("/3/matrixVariable/{path1}/gap/{path2}")
    public boolean f33(@MatrixVariable(name = "arg", pathVar = "path2") String arg){
        //矩阵变量是跟在path变量后的, pathVar指定是哪个path的矩阵变量
        log.info("{}",arg);
        return true;
    }

    //RequestParamMethodArgumentResolver解析RequestParam
    //RequestPartMethodArgumentResolver解析RequestPart
    @PostMapping("/3/requestPart/file")
    public boolean f3411(@RequestParam("arg") String arg, @RequestPart("file") MultipartFile file){
        log.info("{}, {}",arg, file);
        return true;
    }

    //RequestPartMethodArgumentResolver解析普通表单参数
    @PostMapping("/3/requestPart/partArg")
    public boolean f3412(@RequestParam("arg") String arg, @RequestPart("partArg") String partArg){
        log.info("{}, {}",arg, partArg);
        return true;
    }

    //RequestParamMethodArgumentResolver解析普通表单文件
    @PostMapping("/3/requestParam/file")
    public boolean f3413(@RequestParam("file") MultipartFile file){
        log.info("{}",file);
        return true;
    }
    //RequestParamMethodArgumentResolver解析没有@RequestPart的MultipartFile
    @PostMapping("/3/multipartFile")
    public boolean f342(@RequestParam("arg") String arg,  MultipartFile file){
        log.info("{}, {}",arg, file);
        return true;
    }
    //RequestParamMethodArgumentResolver解析没有@RequestPart的MultipartFile
    @PostMapping("/3/part")
    public boolean f343(@RequestParam("arg") String arg,  Part file){
        log.info("{}, {}",arg, file);
        return true;
    }

    //ServletModelAttributeMethodProcessor
    @PostMapping("/3/bean")
    public boolean f344(ArgTest arg){
        log.info("{}",arg);
        return true;
    }


    @PostMapping("/4/bean")
    public boolean f4(@RequestBody  ArgTest arg){
        log.info("{}",arg);
        return true;
    }
}
